package core.gui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.layout.GridData;

import core.utils.ConfigPropertiesHolder;
import core.utils.Property;

// FIXME uruchamiać odpowiednio wcześnie w CKW, OKW i RKW
public class WzdPgDatabaseConfigData extends ValidationWizardPage {
	private Text txtDatabaseUrl;
	private Text txtDatabaseUser;
	private Text txtDatabasePassword;

	/**
	 * Create the wizard.
	 */
	public WzdPgDatabaseConfigData() {
		super("wizardPage");
		setTitle("Set Database Configuration");
		setDescription("Since this is your first usage of this system, database connection input is required.");
	}

	/**
	 * Create contents of the wizard.
	 * @param parent
	 */
	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);

		setControl(container);
		container.setLayout(new GridLayout(2, false));
		
		Label lblDatabaseUrl = new Label(container, SWT.NONE);
		lblDatabaseUrl.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblDatabaseUrl.setText("Database URL");
		
		txtDatabaseUrl = new Text(container, SWT.BORDER);
		txtDatabaseUrl.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label lblDatabaseUser = new Label(container, SWT.NONE);
		lblDatabaseUser.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblDatabaseUser.setText("Database User");
		
		txtDatabaseUser = new Text(container, SWT.BORDER);
		txtDatabaseUser.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label lblDatabasePassword = new Label(container, SWT.NONE);
		lblDatabasePassword.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblDatabasePassword.setText("Database Password");
		
		txtDatabasePassword = new Text(container, SWT.BORDER | SWT.PASSWORD);
		txtDatabasePassword.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
	}

	@Override
	protected void validate() {
		String databaseUrl = txtDatabaseUrl.getText().trim();
		String databaseUser = txtDatabaseUser.getText().trim();
		String databasePassword = txtDatabasePassword.getText().trim();

		// error
		if (databaseUrl.length() == 0) {
			setErrorState("Database Url cannot be empty. Example value: localhost:3306/database-name");
			return;
		}
		if (databaseUser.length() == 0) {
			setErrorState("You must declare user with permission for database.");
			return;
		}
		if (databasePassword.length() == 0) {
			setWarningState("For security reasons, it is highly reccomended to secure users database access with a password.");
			return;
		}

		// ok
		clearErrorState();
	}

	@Override
	protected void update() {
		ConfigPropertiesHolder.getInstance().setProperty(Property.DB_URL, txtDatabaseUrl.getText().trim());
		ConfigPropertiesHolder.getInstance().setProperty(Property.DB_USERNAME, txtDatabaseUser.getText().trim());
		ConfigPropertiesHolder.getInstance().setProperty(Property.DB_PASSWORD, txtDatabasePassword.getText().trim());
	}

}
